package org.leeloo.internal.core;

import org.eclipse.jdt.debug.core.IJavaObject;
import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IViewPart;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.core.Tags;

/**
 * Allows for the add/edit/view of the name of a variable.
 * 
 * @author Brad Reynolds
 */
public class EditNameAction extends AbstractVariablesViewActionDelegate {
    private IViewPart viewPart;
    private IJavaStackFrame stackFrame;
    private IJavaObject javaObject;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
     */
    public void init(IViewPart view) {
        this.viewPart = view;
    }

    public void init(IAction action) {
        action.setEnabled(false);
    }
    
    /**
     * Stores the selection and determines if the action should be enabled.
     * 
     * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
     *      org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        //reset the object state
        this.javaObject = null;
        this.stackFrame = null;
        action.setEnabled(false);
        
        javaObject = getJavaObject(selection);
        stackFrame = getStackFrame(viewPart);
        
        if (javaObject != null) {
            action.setEnabled(true);
        }
    }

    /**
     * @category testability
     * @param name
     * @return dialog to prompt the user with
     */
    protected InputDialog getInputDialog(String name) {
        InputDialog dialog = new InputDialog(viewPart.getSite().getShell(),
                Messages.getString("EditNameAction.dialog_title"), //$NON-NLS-1$
                Messages.getString("EditNameAction.dialog_message"), //$NON-NLS-1$
                name,
                null);

        return dialog;
    }

    public void runWithEvent(IAction action, Event event) {
        assert stackFrame != null : "stackFrame == null"; //$NON-NLS-1$
        assert javaObject != null : "javaObject == null"; //$NON-NLS-1$
        
        // get the tag service
        TagService tagService = getTagService();
        
        ObjectDescriptor descriptor = JavaObjects.getObjectDescriptor(javaObject, stackFrame);
        
        Tag nameTag = tagService.get(descriptor, Tags.NAME);
        String name = (nameTag != null) ? nameTag.getValue() : null;
        
        // prompt the user to enter the name
        InputDialog dialog = getInputDialog(name);
        int result = dialog.open();
        
        if (result == IDialogConstants.OK_ID) {
            // save the new value
            tagService.put(descriptor, new Tag(Tags.NAME, dialog.getValue()));
        }
    }
}